package linter.linterjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import linter.linterjava.POJO.DO.Article;
import linter.linterjava.POJO.DO.ArticleCollect;
import linter.linterjava.POJO.DO.Collect;
import linter.linterjava.POJO.DTO.ArticleCollectDTO;
import linter.linterjava.POJO.UtilDTO.PageInfoDTO;
import linter.linterjava.mapper.ArticleMapper;
import linter.linterjava.mapper.CollectMapper;
import linter.linterjava.service.ArticleCollectService;
import linter.linterjava.mapper.ArticleCollectMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static linter.linterjava.custom.ConstantPublicWay.getList;
import static linter.linterjava.custom.ConstantPublicWay.getPageInfo;
import static linter.linterjava.custom.CustomResultType.*;

/**
* @author 北煜
* @description 针对表【article_collect(文章收藏表)】的数据库操作Service实现
* @createDate 2024-07-11 15:42:55
*/
@Service
public class ArticleCollectServiceImpl extends ServiceImpl<ArticleCollectMapper, ArticleCollect>
    implements ArticleCollectService{

    @Resource
    ArticleCollectMapper articleCollectMapper;
    @Resource
    CollectMapper collectMapper;
    @Resource
    ArticleMapper articleMapper;


    // TODO: 添加合适的Redis缓存注解
    @Override
    public String collect(Long uuid, ArticleCollectDTO articleCollectDTO) {
        // 先判断收藏夹存不存在
        Collect exit = collectMapper.selectById(articleCollectDTO.getCollectId());
        if (exit == null) { return ADD_ERROR; }
        Date date = new Date();
        ArticleCollect articleCollect = new ArticleCollect();
        articleCollect.setArticleId(articleCollectDTO.getArticleId());
        articleCollect.setCollectId(articleCollectDTO.getCollectId());
        articleCollect.setCreateTime(date);
        articleCollect.setUpdataTime(date);
        int flag = articleCollectMapper.insert(articleCollect);
        Article article = new Article();
        Integer collectNub = articleMapper.selectById(articleCollectDTO.getArticleId()).getCollectNub();
        article.setCollectNub(collectNub + 1);
        article.setUpdataTime(date);
        flag += articleMapper.update(article, new QueryWrapper<Article>()
                .eq("id", articleCollectDTO.getArticleId()).eq("is_delete", 0));
        return flag == 2 ? ADD_SUCCESS : ADD_ERROR;
    }

    @Override
    public String deleteCollect(Long uuid, ArticleCollectDTO articleCollectDTO) {
        QueryWrapper<ArticleCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", articleCollectDTO.getArticleId())
                .eq("collect_id", articleCollectDTO.getCollectId());
        ArticleCollect articleCollect = new ArticleCollect();
        articleCollect.setIsDelete(1);
        articleCollect.setUpdataTime(new Date());
        int flag = articleCollectMapper.update(articleCollect, queryWrapper);
        Date date = new Date();
        Article article = new Article();
        Integer collectNub = articleMapper.selectById(articleCollectDTO.getArticleId()).getCollectNub();
        article.setCollectNub(collectNub - 1);
        article.setUpdataTime(date);
        flag += articleMapper.update(article, new QueryWrapper<Article>()
                .eq("id", articleCollectDTO.getArticleId()).eq("is_delete", 0));
        return flag == 2 ? DELETE_SUCCESS : DELETE_ERROR;
    }

    @Override
    public PageInfo<Article> getCollectInArticle(Long uuid, ArticleCollectDTO articleCollectDTO) {
        PageInfoDTO pageInfoDTO = new PageInfoDTO();
        pageInfoDTO.setPageNum(articleCollectDTO.getPageNum());
        pageInfoDTO.setPageSize(articleCollectDTO.getPageSize());
        List<ArticleCollect> list = getList(new QueryWrapper<ArticleCollect>()
                .eq("is_delete",0)
                .eq("collect_id",articleCollectDTO.getCollectId()),
                articleCollectMapper
        );
        // 获取list里面的article_id
        List<Long> articleIds = list.stream().map(ArticleCollect::getArticleId).collect(Collectors.toList());
        return getPageInfo(pageInfoDTO, new QueryWrapper<Article>().eq("is_delete",0).in("id",articleIds),
                articleMapper);
    }
}




